package com.lagou.homework02;

import java.util.*;

public class StudentMapTest {
    public static void main(String[] args) {
        Map<Student,String> m1 = new HashMap<>();
        m1.put(new Student("张飞",35),"朝阳大道");
        m1.put(new Student("关羽",37),"北海大道");
        m1.put(new Student("刘备",38),"中山大道");
        //调用toString方式遍历
        System.out.println("第一种遍历方法");
        System.out.println("m1 = "+m1);
        System.out.println("-------------------------------------------------");
        //通过创建数组保存key值，然后遍历数组
        System.out.println("第二种遍历方法");
        Set<Student> s1 = m1.keySet();
        for (Student std : s1){
            System.out.println(std + ":" + m1.get(std));
        }
        System.out.println("-------------------------------------------------");
        //通过迭代器来遍历
        System.out.println("第三种遍历方法");
        Iterator<Map.Entry<Student,String>> iterator = m1.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<Student,String> entry = iterator.next();
            System.out.println(entry.getKey()+":"+entry.getValue());
        }
        System.out.println("-------------------------------------------------");
        //通过Set方法遍历
        System.out.println("第四种遍历方法");
        Set<Map.Entry<Student,String>> entries = m1.entrySet();
        for (Map.Entry<Student,String> me : entries){
            System.out.println(me);
        }

    }
}
